package com.atguigu.gulimall.order.interceptor;

import com.atguigu.common.constant.AuthServerConstant;
import com.atguigu.common.vo.MemberResponVo;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author ming
 * @create 2022/5/8
 * @description:
 */
public class LoginUserInterceptor implements HandlerInterceptor {
    public  static ThreadLocal<MemberResponVo>  loginUser = new ThreadLocal<MemberResponVo>();
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        boolean match = new AntPathMatcher().match("/order/omsorder/status/**", request.getRequestURI());
        boolean match2 = new AntPathMatcher().match("/payed/**", request.getRequestURI());
        if(match||match2){
           return true;
        }
        Object attribute =  request.getSession().getAttribute(AuthServerConstant.LOGIN_USER);
        if(attribute != null){
            MemberResponVo member = (MemberResponVo) attribute;
            loginUser.set(member);
             return true;
        }else{
            //没登录就去登录
            request.getSession().setAttribute("msg","前先进行登录");
            response.sendRedirect("http://auth.gulimall.com/login.html");
            return false;
        }
    }
}
